package com.cnpinxi.service;

import com.cnpinxi.dao.BindDistributorMapper;
import com.cnpinxi.dao.ProductEntityMapper;
import com.cnpinxi.domain.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by zpc on 2017/3/31.
 */
@Service("productEntityService")
public class ProductEntityServiceImpl extends com.cnpinxi.service.ProductEntityBase implements ProductEntityService {

    @Autowired
    ProductEntityMapper productEntityMapper;
    @Autowired
    ActivityService activityService;
    @Autowired
    DistributionConfigService distributionConfigService;
    @Autowired
    BindDistributorMapper bindDistributorMapper;


    @Override
    public void deleteProductEntity(List<Long> productEntityIdList) {
        super.deleteProductEntity(productEntityIdList);
    }

    @Override
    @Transactional
    public ProductEntity modifyProductEntity(ProductEntity productEntity) {


        if (productEntity.getProductEntityId() == null) {
            super.insertProductEntity(productEntity);
        } else {

            super.updateProductEntity(productEntity);
        }

        return productEntity;
    }


    @Override
    public List<ProductEntity> getProductEntityList(Map<String, String> map) {
        return productEntityMapper.getProductEntityList(map);
    }

    @Override
    public ProductEntity getProductEntityById(Long productEntityId) {
        return productEntityMapper.getProductEntityById(productEntityId);
    }

    @Override
    public ProductEntity getProductEntity(Map<String, String> map) {
        ProductEntity productEntity = productEntityMapper.getProductEntity(map);
        String openId=map.get("openId");
        if(StringUtils.isNotBlank(openId)){
     BindDistributor bindDistributor= bindDistributorMapper.selectBindDistributor(map);

        if (bindDistributor!=null) {
            System.out.println("bindDistributor"+bindDistributor.getParentId());
            DistributionConfig distributionConfig= distributionConfigService.getDistributionConfig(map);
            if(distributionConfig!=null){
                Byte discountsType = distributionConfig.getDiscountsType();
                Integer price = productEntity.getRealPrice();

                if (discountsType == Activity.DISCOUNTSTYPE_REDUCE) {
                    price -= distributionConfig.getDiscountsPrice();
                }
                if (discountsType == Activity.DISCOUNTSTYPE_REDUCE_TO) {
                    price = distributionConfig.getDiscountsPrice();
                }
                productEntity.setRealPrice(price < 0 ? 0 : price);
            }

        }

        }
        return productEntity;
    }
    //根据活动id查询产品
    @Override
    public List<ProductEntity> getProductEntityByActivityId(Long activityId) {
        return productEntityMapper.getProductEntityByActivityId(activityId);
    }

    @Override
    public void reduceProductEntity(Long productEntityId) {
        productEntityMapper.reduceProductEntity(productEntityId);
    }
}
